# decimal 定长
import decimal
from decimal import Decimal, getcontext

# 设置所有长度(整合+小数)为9位
getcontext().prec = 9
r1 = Decimal(1) / Decimal(3)
r2 = Decimal(10) / Decimal(1.5)
print(r1, r2)  # 0.333333333 6.66666667
# 小数位定长,四舍五入
print(r1.quantize(Decimal('0.0000')), r2.quantize(Decimal('0.00')))  # 0.3333 6.67

# 向上取整
getcontext().rounding = getattr(decimal, 'ROUND_CEILING')
print(r1.quantize(Decimal('0.0000')), r2.quantize(Decimal('0.00')))  # 0.3334 6.67

# 向下取整
getcontext().rounding = getattr(decimal, 'ROUND_FLOOR')
print(r1.quantize(Decimal('0.0000')), r2.quantize(Decimal('0.00')))  # 0.3333 6.66

# 浮点数转decimal
print(Decimal.from_float(12.222))  # 12.2219999999999995310417943983338773250579833984375

# decimal转string
print(str(r1.quantize(Decimal('0.0000'))))  # '0.3333'

from fractions import Fraction
from decimal import Decimal
from math import floor, ceil

f = Fraction(1, 5)
f_float = Fraction(1.5)
f_decimal = Fraction(Decimal(1.233))
f_str = Fraction('1/2')
print(f, f_float, f_decimal, f_str)  # 1/5 3/2 2776469170273911/2251799813685248 1/2

print(f.numerator, f.denominator)  # 1 5
# print(f.as_integer_ratio())

print(Fraction.from_float(1.5))  # 3/2
print(Fraction.from_decimal(Decimal(2.5)))  # 5/2
print(Fraction('3.1415926535897932').limit_denominator(1000))  # 355/113
print(floor(Fraction(355, 113)), ceil(Fraction(355, 113)))  # 3 4

import random

print(random.randrange(10), random.randrange(6, 10, 2))
# 随机整数
print(random.randint(1, 10))
# 返回具有 k 个随机比特位的非负 Python 整数
print(random.getrandbits(2))
m = ['a', 'b', 'c', 'd']
# 随机取1个
print(random.choice(m))
# 按权重随机取k个(可能会重复)
print(random.choices(m, weights=[1, 0, 1, 1], k=2))

# 随机打乱
random.shuffle(m)
print(m)

# 随机取k个不重复的元素
print(random.sample(m, 2))
# print(random.sample(m, 2, counts=[1, 1, 1, 2]))
# [0,1) 随机浮点数
print(random.random())
# 返回一个随机浮点数 N
print(random.uniform(1, 10))

# statistics --- 数学统计函数
import statistics

m = [1, 3, 5]
n = [1, 3, 5, 7]
# 算数平均数
print(statistics.mean(m))  # 3

# 将 data 转换成浮点数并且计算算术平均数
# print(statistics.fmean(m))
# 将 data 转换成浮点数并且计算几何平均数
# print(statistics.geometric_mean(m))
# 所有数据的倒数的算术平均数 mean() 的倒数
print(statistics.harmonic_mean(m))  # 1.9565217391304348
# 使用普通的“取中间两数平均值”方法返回数值数据的中位数（中间值
print(statistics.median(m))  # 3
# 数据的低中位数
print(statistics.median_low(m), statistics.median_low(n))  # 3 3
# 数据的高中位数
print(statistics.median_high(m), statistics.median_high(n))  # 3 5
# 返回分组的连续数据的中位数，根据第 50 个百分点的位置使用插值来计算
print(statistics.median_grouped(m), statistics.median_grouped(n))  # 3.0 4.5
# 从离散或标称的 data 返回单个出现最多的数据点
print(statistics.mode([1, 2, 3, 4, 1, 1, 3, 3, 3]),
      statistics.mode(["red", "blue", "blue", "red", "green", "red", "red"]))  # 3 red
# 返回最频繁出现的值的列表，并按它们在 data 中首次出现的位置排序
# print(statistics.multimode('aaannnkkdkiis'), statistics.multimode(''))  # 3 red
# 返回总体标准差（总体方差的平方根）
print(statistics.pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75]))  # 0.986893273527251
# 总体方差
print(statistics.pvariance([0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]))  # 1.25
# 样本标准差（样本方差的平方根）
print(statistics.stdev([0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]))  # 1.1952286093343936
# 样本方差
print(statistics.variance([0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]))  # 1.4285714285714286
# print(statistics.quantiles([0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]))  # 1.4285714285714286
